package com.wnxy.util;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.UUID;

public class CommonsUtil {
    public static <T> Boolean hasAllFields(T t) throws IllegalAccessException {
        if(t==null) return false;
        Class<?> clazz = t.getClass();
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            if (field.get(t)==null) return false;
        }
        return true;
    }
    //随机数字验证码
    public static String getCode(int length){
        Random random = new Random();
        StringBuilder code = new StringBuilder();
        for(int i =0;i<length;i++){
        code.append(random.nextInt(10));
        }
        return code.toString();
    }
    public static void main(String[] args) throws IllegalAccessException {
        String url="/perform/adad/asd";
        System.out.println(url.substring(0, url.indexOf("/", 2)));

    }
    //订单编号
    public static String getOrderNo(){
        return UUID.randomUUID().toString();
    }
    //obj转list
    public static <T> List<T> castList(Object obj, Class<T> clazz)
    {
        List<T> result = new ArrayList<T>();
        if(obj instanceof List<?>)
        {
            for (Object o : (List<?>) obj)
            {
                result.add(clazz.cast(o));
            }
            return result;
        }
        return null;
    }
}
